package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.abstracts.AbstractConditionFieldHandler;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.saas.enums.UserTeamEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/3 19:41
 * @since v1.0
 */
@Component
public class ConditionFormOwnerIdHandlerImpl extends AbstractConditionFieldHandler {

    @Resource
    private FormDataFormExplainHelper formDataFormExplainHelper;


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.OWNERID.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,  FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo ) throws XbbException {
        Integer businessType = businessRuleInfoPojo.getBusinessType();
        Map<Long, List<String>> ownerIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> coUserIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Object object;
        if (UserTeamEnum.hasTeam(businessType)) {
            List<Long> idIn = new ArrayList<>();
            idIn.add(paasFormDataEntity.getId());
            formDataFormExplainHelper.getUserIdMap(idIn, paasFormDataEntity.getCorpid(), businessType, false, ownerIdMap, coUserIdMap);
            object = ownerIdMap.get(paasFormDataEntity.getId());
        } else {
            object = paasFormDataEntity.getOwnerId();
        }
        return object;
    }


    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return filterCondition.getValue();
    }


    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return filterCondition.getValue();
    }
}
